package com.qjq.glasses_shopping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.qjq.glasses_shopping.pojo.Specification;
import com.qjq.glasses_shopping.pojo.SpecificationOptions;
import com.qjq.glasses_shopping.result.BaseResult;
import com.qjq.glasses_shopping.service.SpecificiationService;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
    商品规格服务
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @Resource
    private SpecificiationService specificiationService;

    /**
     * 新增商品规格
     * @param specification 商品规格对象
     * @return 返回执行结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Specification specification) {
        specificiationService.add(specification);
        return BaseResult.ok();
    }

    /**
     * 修改商品规格
     * @param specification 商品规格对象
     * @return 返回执行结果
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Specification specification) {
        specificiationService.update(specification);
        return BaseResult.ok();
    }

    /**
     * 删除商品规格
     * @param ids 商品规格id集合
     * @return 返回执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long[] ids) {
        specificiationService.delete(ids);
        return BaseResult.ok();
    }

    /**
     * 根据id查找商品规格
     * @param id 商品规格id
     * @return 返回查询结果
     */
    @GetMapping("/findById")
    public BaseResult findById(Long id) {
        Specification specification = specificiationService.findById(id);
        return BaseResult.ok(specification);
    }

    /**
     * 分页查询商品规格
     * @param page 页码
     * @param size 每页条数
     * @return 返回查询结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Specification>> search(int page, int size) {
        Page<Specification> page1 = specificiationService.search(page, size);
        return BaseResult.ok(page1);
    }

    /**
     * 根据商品类型id查询商品规格
     * @param id 商品类型id
     * @return 返回查询结果
     */
    @GetMapping("/findByProductTypeId")
    public BaseResult<List<Specification>> findByProductTypeId(Long id) {
        List<Specification> specifications = specificiationService.findByProductTypeId(id);
        return BaseResult.ok(specifications);
    }

    /**
     * 新增商品规格项
     * @param specificationOptions 商品规格项集合
     * @return 返回执行结果
     */
    @PostMapping("/addOption")
    public BaseResult addOption(@RequestBody SpecificationOptions specificationOptions) {
        specificiationService.addOption(specificationOptions);
        return BaseResult.ok();
    }

    /**
     * 删除商品规格项
     * @param ids 商品规格想id集合
     * @return 返回执行结果
     */
    @DeleteMapping("/deleteOption")
    public BaseResult deleteOption(Long[] ids) {
        specificiationService.deleteOption(ids);
        return BaseResult.ok();
    }
}
